<template>
  <div class="search-condition">
    <div class="p-l-10 m-b-10">
      <el-input
        style="width: 300px"
        v-model="nickName"
        :placeholder="placeholder"
        :suffix-icon="Search"
        size="large"
      />
      <el-button size="large" class="m-l-20" plain @click="onDoSearch" type="primary">
        搜索
      </el-button>
      <el-button size="large" plain @click="onDoReset" type="primary">重置</el-button>
    </div>
    <div class="tag-row">
      <div class="tag-title">所属行业:</div>
      <div class="tags">
        <div
          @click="onIndustryTagClick(-1)"
          :class="{ 'tag-active': -1 === industryActiveTagIndex }"
          class="tag c-p"
        >
          全部
        </div>
        <div
          v-for="(item, index) in industrySectorList"
          @click="onIndustryTagClick(index)"
          :class="{ 'tag-active': index === industryActiveTagIndex }"
          :key="item.label"
          class="tag c-p"
        >
          {{ item.label }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
import { industrySectorList } from '@/data/industrySector';
import { ref } from 'vue';

const emit = defineEmits(['doSearch']);
withDefaults(
  defineProps<{
    placeholder?: string;
  }>(),
  {
    placeholder: '请输入讲师姓名',
  }
);

const nickName = ref('');

function onDoReset() {
  nickName.value = '';
  industryActiveTagIndex.value = -1;
  emitEvent();
}

function onDoSearch() {
  emitEvent();
}

// 行业领域
const industryActiveTagIndex = ref(-1);
function onIndustryTagClick(index: number) {
  industryActiveTagIndex.value = index;
  emitEvent();
}
function emitEvent() {
  let industrySector = null;
  if (industryActiveTagIndex.value !== -1) {
    industrySector = industrySectorList[industryActiveTagIndex.value].value;
  }
  emit('doSearch', {
    nickName: nickName.value.trim() ? nickName.value.trim() : null,
    industrySector,
  });
}
</script>

<style lang="scss" scoped>
.search-condition {
  width: 100%;
  .tag-row {
    display: flex;
    flex-wrap: wrap;
    .tag-title {
      width: 100px;
      flex-shrink: 0;
      text-align: center;
      padding: 5px;
    }
    .tags {
      width: 900px;
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      .tag {
        padding: 3px 5px;
        margin: 5px;
        &:hover {
          scale: 1.1;
        }
      }
      .tag-active {
        border: 2px solid #63b0ff;
        background-color: #ecf5ff;
        border-radius: 5px;
      }
    }
  }
}
</style>
